package calao.client;

import calao.client.i18n.Text;
import calao.client.ui.ActionListBox;
import calao.client.ui.Form;
import calao.client.ui.SpacerPanel;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SourcesChangeEvents;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

class DesktopPreferences
extends Form
{

	class ShortcutsEditor 
	extends Grid
	implements SourcesChangeEvents
	{
		final SpacerPanel buttons = new SpacerPanel(SpacerPanel.VERTICAL);
		final Button addButton = new Button(text.add());
		final Button removeButton = new Button(text.remove());
		final Button upButton = new Button(text.moveUp());
		final Button downButton = new Button(text.moveDown());
		final ChangeListenerCollection listeners = new ChangeListenerCollection();
		ShortcutsEditor()
		{
			super(2, 3);
			// buttons panel
			buttons.add(addButton);
			buttons.add(removeButton);
			buttons.add(upButton);
			buttons.add(downButton);
			for (int i=0; i<buttons.getWidgetCount(); i++) {
				buttons.getWidget(i).setWidth("100%");
			}
			// shortcut lists
			activeShortcuts = new ActionListBox() {
				protected void onAction(Button button, int index) { 
					if (button==removeButton) {
						inactiveShortcuts.addItem(getItemText(index), getValue(index));
						removeItem(index);
					}
					else if (button==upButton) {
						activeShortcuts.moveItem(index, -1);
					}
					else if (button==downButton) {
						activeShortcuts.moveItem(index, 1);
					}
					listeners.fireChange(this);
				}
				protected void onSelection() { 
					inactiveShortcuts.setAllSelected(false);
				}
			};
			activeShortcuts.setSize("200", "150");
			activeShortcuts.addActionButton(removeButton);
			activeShortcuts.addActionButton(upButton);
			activeShortcuts.addActionButton(downButton);
			activeShortcuts.setOptions(downButton, true);
			inactiveShortcuts = new ActionListBox() {
				protected void onAction(Button button, int index) { 
					if (button==addButton) {
						activeShortcuts.addItem(getItemText(index), getValue(index));
						removeItem(index);
					}
					listeners.fireChange(this);
				}
				protected void onSelection() { 
					activeShortcuts.setAllSelected(false);
				}
			};
			inactiveShortcuts.setSize("200", "150");
			inactiveShortcuts.addActionButton(addButton);
			// layout widgets
			setText(0, 0, text.activeShortcuts());
			setWidget(1, 0, activeShortcuts);
			setText(0, 2, text.inactiveShortcuts());
			setWidget(1, 2, inactiveShortcuts);
			setWidget(1, 1, buttons);
		}
		public void addChangeListener(ChangeListener listener) {
			listeners.add(listener);		
		}
		public void removeChangeListener(ChangeListener listener) {
			listeners.remove(listener);
		}
	}

	final Desktop desktop = Desktop.getDesktop();
	final Text text = desktop.text;
	final Preferences preferences;

	final ListBox langBox = new ListBox();
	final TextBox titleBox = new TextBox();

	ActionListBox activeShortcuts;
	ActionListBox inactiveShortcuts;

	DesktopPreferences()
	{
		this.preferences = desktop.sessionInfo.getPreferences();
		String[] languages = desktop.getAvailableLanguages();
		for (int i=0; i<languages.length; i++) {
			String lang = languages[i];
			String langName = desktop.getLanguageName(lang);
			if (langName!=null) langBox.addItem(langName, lang);
		}
		add(desktop.text.language(), langBox);
		add(desktop.text.title(), titleBox);
		add(null, new ShortcutsEditor());
		reset();
	}

	public void saveChanges() 
	{
		String[] shortcuts = new String[activeShortcuts.getItemCount()];
		for (int i=0; i<shortcuts.length; i++) {
			shortcuts[i] = activeShortcuts.getValue(i);
		}
		preferences.setLanguage(langBox.getValue(langBox.getSelectedIndex()));
		preferences.setTitle(titleBox.getText());
		preferences.setShortcuts(shortcuts);
		desktop.sessionService.setUserPreferences(preferences, newSaveChangesCallback());
		desktop.refresh();
	}

	public void reset()
	{
		// set language
		String lang = preferences.getLanguage();
		if (lang!=null) {
			for (int i=0; i<langBox.getItemCount(); i++) {
				if (lang.equals(langBox.getValue(i))) {
					langBox.setSelectedIndex(i);
				}
			}
		}
		// set title
		else langBox.setSelectedIndex(0);
		titleBox.setText(preferences.getTitle()!=null? 
				preferences.getTitle() : text.calaoDesktop());
		// set shortcuts
		populate(inactiveShortcuts, listInactiveShortcuts());
		populate(activeShortcuts, desktop.listActiveShortcuts());
	}

	Widget makeTitlePanel(String title, Widget content)
	{
		VerticalPanel panel = new VerticalPanel();
		panel.add(new Label(title));
		panel.add(content);
		return panel;
	}

	void populate(ListBox box, Shortcut[] shortcuts)
	{
		box.clear();
		for (int i=0; i<shortcuts.length; i++) {
			if (shortcuts[i]!=null) {
				box.addItem(shortcuts[i].getTitle(), shortcuts[i].getName());
			}
		}	
	}

	Shortcut[] listInactiveShortcuts()
	{
		Shortcut[] result = desktop.listAvailableShortcuts();
		Shortcut[] active = desktop.listActiveShortcuts();
		for (int i=0; i<result.length; i++) {
			for (int j=0; j<active.length; j++) {
				if (result[i].getName().equals(active[j].getName())) {
					result[i] = null;
					break;
				}
			}
		}		
		return result;
	}

}
